package com.easytouch.vmhdl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.easytouch.core.Configure;
import com.easytouch.core.utils.Log;
import com.easytouch.vmhdl.R;
import com.easytouch.vmhdl.activity.ManagerActivity;

/**
 * Created by gulin on 2018/1/22.
 */

public class ConfigFragment extends BaseMgrFragment implements View.OnClickListener {

    private Log mLogger = new Log("ConfigFragment.txt");

    private CheckBox mCbCash;
    private CheckBox mCbCard;
    private CheckBox mCbMobile;
    private CheckBox mCbFace;
    private Button mBtBack;

    private boolean isChanged = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_config, container, false);
        setTitle(R.string.mgr_config_title);

        mCbCash = view.findViewById(R.id.cb_cash);
        mCbCard = view.findViewById(R.id.cb_card);
        mCbMobile = view.findViewById(R.id.cb_mobile);
        mCbFace = view.findViewById(R.id.cb_face);
        mBtBack = view.findViewById(R.id.bt_back);

        if(Configure.isCash)
        {
            mCbCash.setChecked(true);
        }
        else
        {
            mCbCash.setChecked(false);
        }
        if(Configure.isCard)
        {
            mCbCard.setChecked(true);
        }
        else
        {
            mCbCard.setChecked(false);
        }
        if(Configure.isMobile)
        {
            mCbMobile.setChecked(true);
        }
        else
        {
            mCbMobile.setChecked(false);
        }
        if(Configure.isFace)
        {
            mCbFace.setChecked(true);
        }
        else
        {
            mCbFace.setChecked(false);
        }

        mCbCash.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Configure.isCash = b;
                isChanged = true;
            }
        });
        mCbCard.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Configure.isCard = b;
                isChanged = true;
            }
        });
        mCbMobile.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Configure.isMobile = b;
                isChanged = true;
            }
        });
        mCbFace.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Configure.isFace = b;
                isChanged = true;
            }
        });

        mBtBack.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                if(isChanged)
                {
                    Configure.saveConfig();
                }
                goToFragment(ManagerActivity.FRAGMENT_MENU, null);
                break;
        }
    }
}
